<?php
class Model_TableActualite implements Model_ITable
{
	private static $con = null;
	private static $reqSelectAll = null;
	private static $reqSelectAllLimit = null;
	private static $reqSelectOne = null;
	private static $reqInsert = null;
	private static $reqDeleteOne = null;
	private static $reqUpdate = null;
	private static $reqCount = null;
	
	public static function setConnexion(MyPdo $pdo)
	{
		self::$con = $pdo;
	}
	
	public static function getConnexion()
	{
		return self::$con;
	}
	
	public static function init()
	{
		self::$reqSelectAll = self::$con->prepare('SELECT * FROM actualite ORDER BY date_creation DESC');
		self::$reqSelectAllLimit = self::$con->prepare('SELECT * FROM actualite ORDER BY date_creation DESC LIMIT :begin, :nb');
		self::$reqSelectOne = self::$con->prepare('SELECT * FROM actualite WHERE id_actualite = :idcond');
		self::$reqDeleteOne = self::$con->prepare('DELETE FROM actualite WHERE id_actualite = :idcond');
		self::$reqInsert = self::$con->prepare('INSERT INTO actualite(titre, image, contenu, tags, date_creation, date_modification, publie, id_association, id_evenement, id_commune) VALUES(
		:titre, :image, :contenu, :tags, :datecreation, :datemodification, :publie, :idassociation, :idevenement, :idcommune)
		');
		self::$reqUpdate = self::$con->prepare('UPDATE actualite SET titre = :titre,
			image = :image,
			contenu = :contenu,
			tags = :tags,
			date_creation = :datecreation,
			date_modification = :datemodification,
			publie = :publie,
			id_association = :idassociation,
			id_evenement = :idevenement,
			id_commune = :idcommune WHERE id_actualite = :idcond
		');
		self::$reqCount = self::$con->prepare('SELECT COUNT(*) FROM actualite');
	}

	public static function fetchAll()
	{
		self::$reqSelectAll->execute();
		$results = self::$reqSelectAll->fetchAll();
		return $results;
	}
	
	public static function fetchAllLimit($begin, $nb)
	{
		self::$reqSelectAllLimit->bindParam(':begin', $begin, PDO::PARAM_INT);
		self::$reqSelectAllLimit->bindParam(':nb', $nb, PDO::PARAM_INT);
		self::$reqSelectAllLimit->execute();
		return self::$reqSelectAllLimit->fetchAll();
	}

	public static function fetchOne($id)
	{
		self::$reqSelectOne->execute(array(':idcond' => $id));
		$results = self::$reqSelectOne->fetchAll();
		if( empty($results[0]) )
			return array();
		else
			return $results[0];
	}
	public static function delete($id)
	{
		self::$reqDeleteOne->execute(array(':idcond' => $id));
	}
	
	public static function insert($titre, $image, $contenu, $tags, $datecreation, $datemodification, $publie, $idassociation, $idevenement, $idcommune)
	{
	    self::$reqInsert->execute(array(
			':titre' => $titre,
	    	':image' => $image,
			':contenu' => $contenu,
			':tags' => $tags,
			':publie' => $publie,
			':datecreation' => $datecreation,
			':datemodification' => $datemodification,
			':idassociation' => $idassociation,
			':idevenement' => $idevenement,
	    	':idcommune' => $idcommune
		));
		return self::$con->lastInsertId();
	}
	
	public static function update($id, $titre, $image, $contenu, $tags, $datecreation, $datemodification, $publie, $idassociation, $idevenement, $idcommune)
	{
		self::$reqUpdate->execute(array(
			':titre' => $titre,
			':image' => $image,
			':contenu' => $contenu,
			':tags' => $tags,
			':publie' => $publie,
			':datecreation' => $datecreation,
			':datemodification' => $datemodification,
			':idassociation' => $idassociation,
			':idevenement' => $idevenement,
                        ':idcommune' => $idcommune,
			':idcond' => $id
		));
	}
	
	public static function count()
	{
		self::$reqCount->execute();
		return self::$reqCount->fetchColumn();
	}

}
